<?php
namespace app\index\controller\system;

use app\index\controller\Base;

class learn extends Base
{
    public $tableName = "Learning";
    public function index()
    {
        $type=input("type");     
        $where=[];
        if(!empty($type)){
            $where["mtype"]=$type;
        }
        $this->page($where,10,"","id desc");
       
        $data["file_type"] = model("CodeMap")->get_value("learn_type");
        $data["file_type_item"]=array_column( $data["file_type"],"m_name","m_value");
        $data["type"]= $type; 
        $this->assign($data);
        return view();
    }

    /**
     *  添加
     */
    public function add()
    {
        $model = model('Learning');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
           
           
         
           $files= model("File")->where(["id"=>array("in",$post["file_id"])])->select();
         
           $file_data=[];
           foreach($files as $t_file){          
            $data["mtype"] = $post["mtype"];
            $data["save_name"] = $t_file["save_name"];      
            $data["uid"] =$this->member_id;
            $data["file_name"] = $t_file["file_name"];
            $data["file_path"] = $t_file["save_path"];
            $file_data[]=$data;
           }
           
           //    $data["operate_type"] = $post["operate_type"];
           $result =0;

            $LearningModel = model("Learning");
            if (!empty($file_data)) {
                $result = $LearningModel->saveAll($file_data);
            }
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/system/learn/index"));
        }
   
        $data["file_type"] = model("CodeMap")->get_value("learn_type");
   
        $this->assign($data);
        return view();
    }

    /**
     *  修改
     */
    public function edit()
    {
        $LearningModel = model('Learning');
        if (request()->instance()->isPost()) {
            $post = request()->instance()->param();
        
            $data["save_name"] = $post["save_name"];
       
            $data["mtype"] = $post["mtype"];
            $data["file_type"] = $post["file_type"];
            $data["uid"] = $this->member_id;
            $data["file_name"] = $post["file_name"];
            $data["file_path"] = $post["file_path"];
           
    

            $map["id"] = $post["id"];
            $result = $this->operateData($data, $map);
            if (empty($result)) {
                $this->error("操作失败");

            }
            $this->success("操作成功", url("/Index/system/learn/index"));
        }

        $id = input("id");

        $data["info"] = $LearningModel->get($id);

        $this->assign($data);
        return view();
    }

    public function del()
    {
        $id = input("id");
        if (empty($id)) {
            $this->error("删除失败");
        }
        $result = $this->delBase(["id" => intval($id)]);
        if ($result) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }

}
